package com.atguigu.exam.controller;


import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 轮播图控制器 - 处理轮播图管理相关的HTTP请求
 * 包括图片上传、轮播图的CRUD操作、状态切换等功能
 */
 @Tag(name = "轮播图管理接口")
 @RestController
 @RequestMapping("/api/banners")
public class BannerController {
     @Autowired
    private BannerService bannerService;
    @Autowired
    private FileUploadService fileUploadService;
     @Operation(summary = "获取所有的轮播图信息")
    @GetMapping("/list")
    public Result<List<Banner>> list(){
         //创建LambdaQueryWrapper对象
         LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper=new LambdaQueryWrapper<>();
         //按照排序字段升序排序
         bannerLambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
         //调用BannerService中的list方法
         List<Banner> list=bannerService.list(bannerLambdaQueryWrapper);
         return Result.success(list);
     }
     @Operation(summary = "获取激活状态的轮播图信息")
     @GetMapping("/active")
    public Result activeList(){
         //调用BannerService中的List方法
         List<Banner> list=bannerService.list(new LambdaQueryWrapper<Banner>().eq(Banner::getIsActive,true));
         return Result.success(list);
     }
     @Operation(summary = "根据id获取轮播图信息")
     @GetMapping("/{id}")
    public Result<Banner> getById(@PathVariable Long id){
         //调用BannerService中的getById方法
         Banner banner=bannerService.getById(id);
         return Result.success(banner);
     }

//   @Operation(summary = "添加轮播图信息")
//    @PostMapping("/add")
//    public Result add(@RequestBody Banner banner){
//        //调用BannerService中的save方法
//        boolean save = bannerService.save(banner);
//        if(save){
//            return Result.success("添加成功");
//        }else{
//            return Result.error("添加失败");
//        }
//    }
//
//    @Operation(summary = "更新轮播图信息")
//    @PutMapping("/update")
//    public Result update(@RequestBody Banner banner){
//        //调用BannerService中的updateById方法
//        boolean update = bannerService.updateById(banner);
//        if(update){
//            return Result.success("更新成功");
//        }else{
//            return Result.error("更新失败");
//        }
//    }

    @Operation(summary = "根据id删除轮播图信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        //调用BannerService中的removeById方法
        boolean b = bannerService.removeById(id);
        if(b){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

    @Operation(summary = "添加或更新轮播图信息")
    @RequestMapping(value = {"/add","/update"},method = {RequestMethod.POST,RequestMethod.PUT})
    public Result saveOrUpdate(@RequestBody Banner banner){
        //调用BannerService中的saveOrUpdate方法
        boolean b = bannerService.saveOrUpdate(banner);
        if(b){
            return Result.success("保存或更新成功");
        }else{
            return Result.error("保存或更新失败");
        }
    }
    @Operation(summary = "上传轮播图")
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file){
         //调用FileUploadService中的uploadFile方法
        String url=fileUploadService.uploadFile(file);
        return Result.success(url,"上传轮播图成功");
    }
    @Operation(summary = "更新轮播图的激活状态")
    @PutMapping("/toggle/{id}")
    public Result toggle(@PathVariable Long id, @RequestParam Boolean isActive){
         //创建LambdaUpdateWrapper对象
        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //设置要更新的字段
        bannerLambdaUpdateWrapper.set(Banner::getIsActive,isActive).set(Banner::getUpdateTime,new Date());
        //设置查询条件
        bannerLambdaUpdateWrapper.eq(Banner::getId,id);
        //调用BannerService中的update方法
        boolean update=bannerService.update(bannerLambdaUpdateWrapper);
        if(update){
            return Result.success("更新激活状态成功");
        }else{
            return Result.error("更新激活状态失败");
        }
    }


} 